﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using FarseerGames.FarseerPhysics;
using FarseerGames.FarseerPhysics.Mathematics;

namespace Demo6
{
    public partial class MainPage : UserControl
    {
        private PhysicsSimulator physicsSimulator;
        private SpaceShip ship;
        private KeyHandler keyHandler;

        public MainPage()
        {
            InitializeComponent();

            //Hook up the KeyHandler to listen to the MainPage
            keyHandler = new KeyHandler();
            keyHandler.Attach(this);

            //Setup the PhysicsSimulator
            physicsSimulator = new PhysicsSimulator(new Vector2(0, 100)); //Change 100 to 0 if you want "Asteroid-style" game (no gravity)
            physicsSimulator.Iterations = 25;

            //Instantiate the space ship 
            ship = new SpaceShip(20,20, 20, keyHandler);
            ship.Body.Position = new Vector2((float)800 / 2, (float)600 / 2); //This will center the ship on the game screen

            //Add SpaceShip to physicsSimulator
            physicsSimulator.Add(ship.Body);

            //Add the SpaceShip to the GameCanvas
            GameCanvas.Children.Add(ship);

            //Hook up "GameLoop trigger" - We don't use Storyboards anymore, like in the older simple samples
            CompositionTarget.Rendering += new EventHandler(CompositionTarget_Rendering);

        }

        protected void CompositionTarget_Rendering(object sender, EventArgs e)
        {
            physicsSimulator.Update(.01f);

            ship.Update();
        }
    }
}
